



#include "user_uart_interrupt.h"
#include "user_gpio.h"
#include "MHL10.h" 
#include "stdio.h" 
#include <string.h> 
#include "main.h"
#include "BC26.h"
#include "user_delay.h"
#include "bstim32_timing.h"

extern uint8_t UART0_Rx_Buf[MAX_REC_LENGTH];
extern uint16_t  UART0_Rx_cnt ;
extern uint8_t UART0_temp[REC_LENGTH];



extern uint8_t MHL10_TxData[50];
extern uint8_t LoRa_Detect;
extern uint8_t LoRa_Config; 

uint8_t MHL10_ADDR[9];
uint8_t MHL10_ADDR_Valid[8];

extern uint8_t Rece_len;
extern uint8_t Recebuffer[ReceiveUdpDataMaxLen];


uint8_t S1_MHL10_TxFreq_cmd[19+1]={'A','T','+','T','F','R','E','Q','=','1','9','C','F','0','E','4','0','\r','\n',0}; 
uint8_t S1_MHL10_RxFreq_cmd[19+1]={'A','T','+','R','F','R','E','Q','=','1','9','C','F','0','E','4','0','\r','\n',0}; 
uint8_t M1_MHL10_TxFreq_cmd[19+1]={'A','T','+','T','F','R','E','Q','=','1','C','0','3','A','1','8','0','\r','\n',0}; 
uint8_t M1_MHL10_RxFreq_cmd[19+1]={'A','T','+','R','F','R','E','Q','=','1','D','1','6','4','A','0','0','\r','\n',0}; 
uint8_t M2_MHL10_TxFreq_cmd[19+1]={'A','T','+','T','F','R','E','Q','=','1','C','3','1','6','8','4','0','\r','\n',0}; 
uint8_t M2_MHL10_RxFreq_cmd[19+1]={'A','T','+','R','F','R','E','Q','=','1','D','4','4','1','0','C','0','\r','\n',0}; 
uint8_t W1_MHL10_TxFreq_cmd[19+1]={'A','T','+','T','F','R','E','Q','=','1','C','5','F','2','F','0','0','\r','\n',0}; 
uint8_t W1_MHL10_RxFreq_cmd[19+1]={'A','T','+','R','F','R','E','Q','=','1','D','7','1','D','7','8','0','\r','\n',0}; 
uint8_t W2_MHL10_TxFreq_cmd[19+1]={'A','T','+','T','F','R','E','Q','=','1','C','8','C','F','5','C','0','\r','\n',0}; 
uint8_t W2_MHL10_RxFreq_cmd[19+1]={'A','T','+','R','F','R','E','Q','=','1','D','9','F','9','E','4','0','\r','\n',0}; 
uint8_t N1_MHL10_TxFreq_cmd[19+1]={'A','T','+','T','F','R','E','Q','=','1','C','B','A','B','C','8','0','\r','\n',0}; 
uint8_t N1_MHL10_RxFreq_cmd[19+1]={'A','T','+','R','F','R','E','Q','=','1','D','C','D','6','5','0','0','\r','\n',0}; 
uint8_t N2_MHL10_TxFreq_cmd[19+1]={'A','T','+','T','F','R','E','Q','=','1','C','E','8','8','3','4','0','\r','\n',0}; 
uint8_t N2_MHL10_RxFreq_cmd[19+1]={'A','T','+','R','F','R','E','Q','=','1','D','F','B','2','B','C','0','\r','\n',0}; 
	
	
	
/*
*********************************************************************************************************
*	������:   MHL10_Initial
*	����˵��: MHL10ģ���ʼ��
*	����ֵ:   ��
*   ���ߣ�    ����
*   ���ڣ�    2020-10-30
*********************************************************************************************************/
void MHL10_Initial(void)
{
	SysPrintf("MHL10 Initial MCU-IO...\r\n");
	NB_PW_OFF;
	HAL_Delay(1500);
	NB_PW_ON; 
	RSTn_HIGH;
	HAL_Delay(3000);
	
	RSTn_LOW;
	HAL_Delay(100);
	RSTn_HIGH;
	
	WAKE_UP_LOW;
	AT_HIGH;
	HAL_Delay(200);
	AT_HIGH;
	HAL_Delay(3000);
	
	
	SysPrintf("len=%d\r\n",UART0_Rx_cnt);
}

/*
*********************************************************************************************************
*	������:   MHL10_Addr_ON
*	����˵��: ��MHL10ģ���ַ
*	����ֵ:   ��
*   ���ߣ�    ����
*   ���ڣ�    2020-10-29
*********************************************************************************************************/
void MHL10_Addr_ON(void)
{
	uint8_t MHL10_Addr_ON_cmd[11+1]={'A','T','+','S','I','P','=','0','1','\r','\n',0};
	SysPrintf("MHL10 open Addr...\r\n");
	UART0_Rx_cnt=0 ;
	SendDataToLoRaModule(MHL10_Addr_ON_cmd,11);
	HAL_Delay(200);
	SysPrintf("len=%d\r\n",UART0_Rx_cnt);
	

	UART0_Rx_Buf[UART0_Rx_cnt]=0 ;

	SysPrintf("cjf debug data--%s\n",UART0_Rx_Buf);
}


void MHL10_Addr_OFF(void)
{
	uint8_t MHL10_Addr_OFF_cmd[11+1]={'A','T','+','S','I','P','=','0','0','\r','\n',0};
	SysPrintf("MHL10 close Addr...\r\n");
	
	UART0_Rx_cnt=0 ;
	
	SendDataToLoRaModule(MHL10_Addr_OFF_cmd,11);
	HAL_Delay(200);
	SysPrintf("len=%d\r\n",UART0_Rx_cnt);
	

	UART0_Rx_Buf[UART0_Rx_cnt]=0 ;

	
	SysPrintf("%s\n",UART0_Rx_Buf);
}

/*
*********************************************************************************************************
*	������:   Get_MHL10_Addr
*	����˵��: ��ȡMHL10ģ���ַ
*	����ֵ:   ��
*   ���ߣ�    ����
*   ���ڣ�    2020-10-29
*********************************************************************************************************/
void Get_MHL10_Addr(void)
{
	
		uint8_t i,j,k=3;
		uint8_t Get_MHL10_Addr_cmd[10+1]={'A','T','+','A','D','D','R','?','\r','\n',0};
		SysPrintf("Get MHL10  Addr...\r\n");

	
	  while(k--)
    {
			UART0_Rx_cnt=0 ;

			SendDataToLoRaModule(Get_MHL10_Addr_cmd,10);
			HAL_Delay(1000);
			SysPrintf("len=%d\r\n",UART0_Rx_cnt);
			
			UART0_Rx_Buf[UART0_Rx_cnt]=0;

			SysPrintf("addr %s\r\n",UART0_Rx_Buf);
			
			for(i=0;i<UART0_Rx_cnt;i++)
			{
				if((UART0_Rx_Buf[i]=='A')&&(UART0_Rx_Buf[i+1]=='D')&&(UART0_Rx_Buf[i+2]=='D')&&(UART0_Rx_Buf[i+3]=='R') && (UART0_Rx_Buf[i+4]==':'))
				{
					for(j=0;j<8;j++)
					{
						MHL10_TxData[8+j] = UART0_Rx_Buf[i+5+j];
						MHL10_ADDR[j] = UART0_Rx_Buf[i+5+j];
						MHL10_ADDR_Valid[j] =  UART0_Rx_Buf[i+5+j];
					}
					 MHL10_ADDR[j] = 0;
					
					SysPrintf("get cjf mhl10 addr%s\n",UART0_Rx_Buf);  

					return;
				}
			}
			
			







		  }

}


/*
*********************************************************************************************************
*	������:   Enter_MHL10_Config_Mode
*	����˵��: ʹMHL10��������ģʽ
*	����ֵ:   ��
*   ���ߣ�    ����
*   ���ڣ�    2020-10-30
*********************************************************************************************************/
void Enter_MHL10_Config_Mode(void)
{
	SysPrintf("MHL10 enter config mode...\r\n");
	AT_HIGH;
	HAL_Delay(200);
}

/*
*********************************************************************************************************
*	������:   Quit_MHL10_Config_Mode
*	����˵��: ʹMHL10��������ģʽ
*	����ֵ:   ��
*   ���ߣ�    ����
*   ���ڣ�    2020-10-30
*********************************************************************************************************/
void Quit_MHL10_Config_Mode(void)
{
	SysPrintf("MHL10 quit config mode...\r\n");
	AT_LOW;

	HAL_Delay(200);
}


/*
*********************************************************************************************************
*	������:   Set_MHL10_Node_To_Node
*	����˵��: ��Ե㴫��
*	����ֵ:   ��
*   ���ߣ�    ����
*   ���ڣ�    2020-10-30
*********************************************************************************************************/
void Set_MHL10_Node_To_Node(void)
{
	uint8_t MHL10_NodeToNode_cmd[11+1]={'A','T','+','N','E','T','=','0','1','\r','\n',0};
	SysPrintf("Set MHL10 node to node...\r\n");

	UART0_Rx_cnt=0;
	SendDataToLoRaModule(MHL10_NodeToNode_cmd,11);
	HAL_Delay(1000);
	SysPrintf("len=%d\r\n",UART0_Rx_cnt);

	UART0_Rx_Buf[UART0_Rx_cnt]=0 ;

	
	SysPrintf("%s\n",UART0_Rx_Buf);
}



/*
*********************************************************************************************************
*	������:   Set_MHL10_RF_Freq
*	����˵��: ����MHL10����ƵƵ��
*	����ֵ:   ��
*   ���ߣ�    ����
*   ���ڣ�    2020-10-30
*********************************************************************************************************/
void Set_MHL10_RF_Freq(uint8_t ch)
{
	



	uint8_t MHL10_TxFreq_cmd[19+1]={'A','T','+','T','F','R','E','Q','=','1','C','1','7','7','7','A','0','\r','\n',0}; 
	uint8_t MHL10_RxFreq_cmd[19+1]={'A','T','+','R','F','R','E','Q','=','1','D','E','1','3','B','2','0','\r','\n',0}; 

	SysPrintf("Set MHL10 RF Freq...\r\n");

	UART0_Rx_cnt=0 ;
	if(ch==1)
		SendDataToLoRaModule(M1_MHL10_TxFreq_cmd,19);
	else if(ch==2)
		SendDataToLoRaModule(M2_MHL10_TxFreq_cmd,19);
	else if(ch==3)
		SendDataToLoRaModule(W1_MHL10_TxFreq_cmd,19);
	else if(ch==4)
		SendDataToLoRaModule(W2_MHL10_TxFreq_cmd,19);
	else if(ch==5)
		SendDataToLoRaModule(N1_MHL10_TxFreq_cmd,19);
	else if(ch==6)
		SendDataToLoRaModule(N2_MHL10_TxFreq_cmd,19);
	else if(ch==7)
		SendDataToLoRaModule(MHL10_TxFreq_cmd,19);   
	else 
		SysPrintf("para err...\r\n");
	
	HAL_Delay(1000);
	SysPrintf("len=%d\r\n",UART0_Rx_cnt);

	UART0_Rx_Buf[UART0_Rx_cnt]=0 ;

	SysPrintf("%s\n",UART0_Rx_Buf);
	UART0_Rx_cnt = 0;
	if(ch==1)
		SendDataToLoRaModule(M1_MHL10_RxFreq_cmd,19);
	else if(ch==2)
		SendDataToLoRaModule(M2_MHL10_RxFreq_cmd,19);
	else if(ch==3)
		SendDataToLoRaModule(W1_MHL10_RxFreq_cmd,19);
	else if(ch==4)
		SendDataToLoRaModule(W2_MHL10_RxFreq_cmd,19);
	else if(ch==5)
		SendDataToLoRaModule(N1_MHL10_RxFreq_cmd,19);
	else if(ch==6)
		SendDataToLoRaModule(N2_MHL10_RxFreq_cmd,19);
	else if(ch==7)
		SendDataToLoRaModule(MHL10_RxFreq_cmd,19);

	else 
		SysPrintf("para err...\r\n");
	HAL_Delay(1000);
	SysPrintf("len=%d\r\n",UART0_Rx_cnt);

	UART0_Rx_Buf[UART0_Rx_cnt]=0 ;

	SysPrintf("%s\n",UART0_Rx_Buf);
}


/*
*********************************************************************************************************
*	������:   Close_MHL10_RIQ
*	����˵��: �رս��շ�ת
*	����ֵ:   ��
*   ���ߣ�    ����
*   ���ڣ�    2020-10-30
*********************************************************************************************************/
void Close_MHL10_RIQ(void)
{
	uint8_t MHL10_CloseRIQ_cmd[11+1]={'A','T','+','R','I','Q','=','0','0','\r','\n',0};
	SysPrintf("Clsoe MHL10 RIQ...\r\n");

	UART0_Rx_cnt=0;

	SendDataToLoRaModule(MHL10_CloseRIQ_cmd,11);
	HAL_Delay(200);
	SysPrintf("len=%d\r\n",UART0_Rx_cnt);

	UART0_Rx_Buf[UART0_Rx_cnt]=0 ;

	SysPrintf("%s\n",UART0_Rx_Buf);
}




void Close_MHL10_TIQ(void)
{
	uint8_t MHL10_CloseTIQ_cmd[11+1]={'A','T','+','T','I','Q','=','0','0','\r','\n',0};
	
	  SysPrintf("Clsoe MHL10 TIQ...\r\n");

		UART0_Rx_cnt=0;
		SendDataToLoRaModule(MHL10_CloseTIQ_cmd,11);
		HAL_Delay(200);
		SysPrintf("len=%d\r\n",UART0_Rx_cnt);
    UART0_Rx_Buf[UART0_Rx_cnt]=0 ;

	  SysPrintf("%s\n",UART0_Rx_Buf);


}





void Set_MHL10_BRATE(void)
{
	uint8_t Set_MHL10_BRATE_cmd[13+1]={'A','T','+','B','R','A','T','E','=','0','7','\r','\n',0};
	SysPrintf("Set MHL10 Brate=115200...\r\n");

	UART0_Rx_cnt=0 ;

	SendDataToLoRaModule(Set_MHL10_BRATE_cmd,13);
	HAL_Delay(20);
	
	/*
	MX_UART5_Init(); 
	HAL_UART_Receive_IT(&huart5,(uint8_t *)UART1_temp,REC_LENGTH);
	HAL_Delay(1000);
	SysPrintf("len=%d\r\n",UART0_Rx_cnt);
	UART0_Rx_Buf[UART0_Rx_cnt] = 0;
	SysPrintf("%s\n",UART0_Rx_Buf);
	*/
	
}




void Wakeup_MHL10_HW(void)
{
	SysPrintf("Wakeup MHL10 ...\r\n");
	WAKE_UP_HIGH;
	HAL_Delay(10);
	WAKE_UP_LOW;
}







void Sleep_MHL10_SW(void)
{

		uint8_t Set_MHL10_Sleep_cmd[13+1]={'A','T','+','L','C','P','=','0','7','0','8','\r','\n',0};


		SysPrintf("Sleep MHL10 SW...\r\n");
		Enter_MHL10_Config_Mode();	

		UART0_Rx_cnt=0 ;

		SendDataToLoRaModule(Set_MHL10_Sleep_cmd,13); 
			
		HAL_Delay(500);
		SysPrintf("len=%d\r\n",UART0_Rx_cnt);
		
		UART0_Rx_Buf[UART0_Rx_cnt]=0;

		SysPrintf("%s\n",UART0_Rx_Buf);
		if(strncmp(UART0_Rx_Buf,"\r\nOK",2)==0)
		{
				SysPrintf("lora sleep OK \r\n");
		}
		else
		{
		    SysPrintf("lora sleep fail \r\n");
		}
		
		
		Quit_MHL10_Config_Mode();
		

}






/*
*********************************************************************************************************
*	������:   Get_MHL10_Config_Info
*	����˵��: ��ȡ������Ϣ
*	����ֵ:   ��
*   ���ߣ�    ����
*   ���ڣ�    2020-10-30
*********************************************************************************************************/
void Get_MHL10_Config_Info(void)
{
		uint16_t i;
uint8_t Get_MHL10_Config_cmd[9+1]={'A','T','+','C','F','G','?','\r','\n',0};
	
	SysPrintf("Get MHL10 Config-Info...\r\n");

	UART0_Rx_cnt=0 ;

	SendDataToLoRaModule(Get_MHL10_Config_cmd,9);
	HAL_Delay(1500);
	SysPrintf("len=%d\r\n",UART0_Rx_cnt);

	UART0_Rx_Buf[UART0_Rx_cnt]=0 ;
	for(i=0;i<UART0_Rx_cnt;i++)
		SysPrintf("%c",UART0_Rx_Buf[i]);
	SysPrintf("\r\n");
	
}


void Set_MHL10_TYPE(void)
{
	uint8_t Set_MHL10_TYPE_cmd[12+1]={'A','T','+','T','Y','P','E','=','0','0','\r','\n',0};
	
	SysPrintf("Set MHL10 TYPE=00...\r\n");

	UART0_Rx_cnt=0 ;

	SendDataToLoRaModule(Set_MHL10_TYPE_cmd,12);
	HAL_Delay(200);
	SysPrintf("len=%d\r\n",UART0_Rx_cnt);

	UART0_Rx_Buf[UART0_Rx_cnt]=0 ;

	SysPrintf("%s\n",UART0_Rx_Buf);
	
}



void Set_MHL10_LCP(void)
{
	uint8_t Set_MHL10_LCP_cmd[13+1]={'A','T','+','L','C','P','=','F','F','F','F','\r','\n',0};
	
	SysPrintf("Set MHL10 LCP=65535s...\r\n");

	UART0_Rx_cnt=0;

	SendDataToLoRaModule(Set_MHL10_LCP_cmd,13);
	HAL_Delay(200);
	SysPrintf("len=%d\r\n",UART0_Rx_cnt=0);

	UART0_Rx_Buf[UART0_Rx_cnt]=0 ;

	SysPrintf("%s\n",UART0_Rx_Buf);
}


void Set_MHL10_RXW(void)
{
	uint8_t Set_MHL10_RXW_cmd[11+1]={'A','T','+','R','X','W','=','0','9','\r','\n',0};	
	SysPrintf("Set MHL10 RXW=9s...\r\n");
	UART0_Rx_cnt=0 ;

	SendDataToLoRaModule(Set_MHL10_RXW_cmd,11);
	HAL_Delay(200);
	SysPrintf("len=%d\r\n",UART0_Rx_cnt);

	UART0_Rx_Buf[UART0_Rx_cnt]=0 ;

	SysPrintf("%s\n",UART0_Rx_Buf);
}



void Set_MHL10_SF(void)
{
	uint8_t Set_MHL10_RSF_cmd[11+1]={'A','T','+','R','S','F','=','0','9','\r','\n',0};
	uint8_t Set_MHL10_TSF_cmd[11+1]={'A','T','+','T','S','F','=','0','9','\r','\n',0};
	SysPrintf("Set MHL10  RSF=5,TSF=5...\r\n");

	UART0_Rx_cnt=0 ;

	SendDataToLoRaModule(Set_MHL10_RSF_cmd,11);
	HAL_Delay(200);
	SysPrintf("len=%d\r\n",UART0_Rx_cnt);
	
	UART0_Rx_Buf[UART0_Rx_cnt]=0 ;
	
	SysPrintf("%s\n",UART0_Rx_Buf);
	UART0_Rx_cnt=0 ;

	SendDataToLoRaModule(Set_MHL10_TSF_cmd,11);
	HAL_Delay(200);
	SysPrintf("len=%d\r\n",UART0_Rx_cnt);
	UART0_Rx_Buf[UART0_Rx_cnt]=0 ;

	SysPrintf("%s\n",UART0_Rx_Buf);
}


void Set_MHL10_BW(void)
 {
	uint8_t Set_MHL10_BW_cmd[10+1]={'A','T','+','B','W','=','0','7','\r','\n',0};
	
	SysPrintf("Set MHL10 BW=125KHZ...\r\n");

	UART0_Rx_cnt=0 ;

	SendDataToLoRaModule(Set_MHL10_BW_cmd,10);
	HAL_Delay(200);
	SysPrintf("len=%d\r\n",UART0_Rx_cnt);
	
	UART0_Rx_Buf[UART0_Rx_cnt]=0 ;

	SysPrintf("%s\n",UART0_Rx_Buf);

}

uint8_t Detect_MHL10_Baund(void)
{
	uint8_t Get_MHL10_ID_cmd[8+1]={'A','T','+','I','D','?','\r','\n',0};
	SysPrintf("Get MHL10 ID use 115200-baund...\r\n");
	
	UART0_Rx_cnt=0 ;
	SendDataToLoRaModule(Get_MHL10_ID_cmd,8);
	HAL_Delay(1000);
	
	SysPrintf("len=%d\r\n",UART0_Rx_cnt);  

	UART0_Rx_Buf[UART0_Rx_cnt]=0 ;


	SysPrintf("%s\n",UART0_Rx_Buf);              
	if(strstr(UART0_Rx_Buf,"+ID:")!=NULL) 
    {
		    SysPrintf("MHL10 banud is 115200\r\n");
        return 1 ;
    }
    else 
    {
		    SysPrintf("Reinitial MCU UART5 9600-baund...\r\n");
			


			





				UART0_Rx_cnt=0 ;                            

				SendDataToLoRaModule(Get_MHL10_ID_cmd,8);
				HAL_Delay(1000);
				SysPrintf("len=%d\r\n",UART0_Rx_cnt);
			
				UART0_Rx_Buf[UART0_Rx_cnt]=0;      	


				SysPrintf("%s\n",UART0_Rx_Buf);
				if(strstr(UART0_Rx_Buf,"+ID:")!=NULL)
				{
					SysPrintf("MHL10 banud is 9600\r\n");
					return 2 ;
				}
				else
				{
					SysPrintf("MHL10 not exist\r\n");
					return 0 ;
				}

    }
}



uint8_t Check_MHL10(void)
{
	
	uint8_t retry=3,status;
	uint8_t Get_MHL10_ID_cmd[8+1]={'A','T','+','I','D','?','\r','\n',0};
	SysPrintf("���MHL10�Ƿ�����\r\n");
	
	while(retry--)	
	{
		
		    MHL10_Initial();
				UART0_Rx_cnt=0 ;
				SendDataToLoRaModule(Get_MHL10_ID_cmd,8);
				HAL_Delay(1000);
			
				SysPrintf("len=%d\r\n",UART0_Rx_cnt);  
				UART0_Rx_Buf[UART0_Rx_cnt]=0 ;

				SysPrintf("%s\n",UART0_Rx_Buf);  


				if(strstr(UART0_Rx_Buf,"+ID:")!=NULL)
				{
						SysPrintf("MHL10 is ok\r\n");
					  status=TRUE;
					  break;

				}
				else 
				{
						SysPrintf("MHL10 is unknown\r\n");
					  status=FALSE;
					  break;
						
				}

	}	
	
	return status;	
	
}





/*
*********************************************************************************************************
*	������:   MHL10_Initial_Config
*	����˵��: ��ʼ��������ģ��
*	����ֵ:   ��
*   ���ߣ�    ����
*   ���ڣ�    2020-10-30
*********************************************************************************************************/

void MHL10_Initial_Config(void)
{
	uint8_t MHL10_Baund;
	MHL10_Initial();
	
	Enter_MHL10_Config_Mode();
	if(Check_MHL10()==FALSE)
	{
	  LoRa_Detect = 0; 
		NB_PW_OFF;      
	}
  else
	{
	  if(LoRa_Config==1)
		{
			Set_MHL10_Node_To_Node();
			Set_MHL10_RF_Freq(7);
		
			Close_MHL10_RIQ();
			Close_MHL10_TIQ();

			MHL10_Addr_OFF();     
			Get_MHL10_Addr();
			Set_MHL10_BW();
			Set_MHL10_SF();
			Set_MHL10_LCP();
			Set_MHL10_RXW();
			LoRa_Config = 0;
		}
		Get_MHL10_Addr();
		Get_MHL10_Config_Info();
		
		LoRa_Detect = 1;
	}
	
	 Quit_MHL10_Config_Mode();
	
	Sleep_MHL10_SW();		
	HAL_Delay(100);
}




uint8_t MHL10_Sent_Packet_To_Gateway(uint8_t *buf,uint8_t len)
{
	    uint8_t i=5;
			uint16_t j=0;
			uint16_t k,begin_pos,end_pos;
			Wakeup_MHL10_HW();
			HAL_Delay(50);
	
			SysPrintf("MHL10  Sent packet to Server...\r\n");
			for(j=0;j<MAX_REC_LENGTH;j++)
				UART0_Rx_Buf[j] = 0;
			UART0_Rx_cnt=0 ;
			
			UART0_SentData(buf,len);
			UART0_Rx_cnt=0 ;
  	  while(i--)
      {
					HAL_Delay(2000);
					SysPrintf("len=%d\r\n",UART0_Rx_cnt);
					if(UART0_Rx_cnt>0)
					{						
						SysPrintf("len=%d\r\n",UART0_Rx_cnt);	
						SysPrintf("Recv data:");
						for(j=0;j<UART0_Rx_cnt;j++)
							SysPrintf("%c",UART0_Rx_Buf[j]);
						SysPrintf("\r\n");
					}
					
					if( (strstr(UART0_Rx_Buf,"OK")!=NULL) ) 
					{
						if( (UART0_Rx_Buf[0]==MHL10_ADDR[0]) && (UART0_Rx_Buf[1]==MHL10_ADDR[1]) &&(UART0_Rx_Buf[2]==MHL10_ADDR[2]) &&(UART0_Rx_Buf[3]==MHL10_ADDR[3]) &&(UART0_Rx_Buf[4]==MHL10_ADDR[4]) &&(UART0_Rx_Buf[5]==MHL10_ADDR[5]) &&(UART0_Rx_Buf[6]==MHL10_ADDR[6]) &&(UART0_Rx_Buf[7]==MHL10_ADDR[7]))
						SysPrintf("return data OK \r\n");
						UART0_Rx_cnt = 0;
						return 1;
					}
					else if( (strstr(UART0_Rx_Buf,"AA")!=NULL) ) 
					{
						if( (UART0_Rx_Buf[0]==MHL10_ADDR[0]) && (UART0_Rx_Buf[1]==MHL10_ADDR[1]) &&(UART0_Rx_Buf[2]==MHL10_ADDR[2]) &&(UART0_Rx_Buf[3]==MHL10_ADDR[3]) &&(UART0_Rx_Buf[4]==MHL10_ADDR[4]) &&(UART0_Rx_Buf[5]==MHL10_ADDR[5]) &&(UART0_Rx_Buf[6]==MHL10_ADDR[6]) &&(UART0_Rx_Buf[7]==MHL10_ADDR[7]) &&(UART0_Rx_Buf[8]==','))
						{
							begin_pos = 9;
						}
						else
						{
							SysPrintf("err addr format\r\n");
							return 3;
						}
						/* if(k==UART0_Rx_cnt-5)
						{
							SysPrintf("err addr format\r\n");
							return 3;
						} */
						if( (UART0_Rx_Buf[UART0_Rx_cnt-1]=='A') && (UART0_Rx_Buf[UART0_Rx_cnt-2]=='A') )
						{
							end_pos = UART0_Rx_cnt -1;
						}
						else
						{
							SysPrintf("err cmd content format\r\n");
							return 4;
						}
						
						Rece_len = UART0_Rx_cnt - begin_pos;
						if(Rece_len<ReceiveUdpDataMaxLen)
						{
							for(k=0;k<Rece_len;k++)
							{
								Recebuffer[k] = UART0_Rx_Buf[begin_pos+k];
							}
						}
						else
						{
							Rece_len = 0;
							SysPrintf("err cmd len exceed max len\r\n");
							return 5;
						}
						
						SysPrintf("Recv config cmd:%s\r\n",Recebuffer);
						return 2;
					}
						






		   }
			UART0_Rx_cnt = 0;
			return 0;
}





uint8_t ConvertStringToHex(uint8_t *str)
{
	uint8_t hightbyte, lowbyte;
	if (str[0] >= 'a')
	{
		hightbyte = (str[0] - 'a' + 10) * 16;
	}
	else
	{
		hightbyte = (str[0] - '0') * 16;
	}
	if (str[1] >= 'a')
	{
		lowbyte = str[1] - 'a' + 10;
	}
	else
	{
		lowbyte = str[1] - '0';
	}
	return hightbyte + lowbyte;
}




uint16_t ReceiveCmdFromServer(uint8_t*Recebuffer)
{
	
	uint8_t datalength = 0;
	uint8_t *start, *next, i = 0, j = 0;
	uint8_t		buf[100]={0};

	if(UART0_Rx_cnt>0)
	{
		UART0_Rx_Buf[UART0_Rx_cnt]=0;

				
				#if 1
				start = strstr(UART0_Rx_Buf, "OK+RECV:");
				if (start != NULL)
				{
						memcpy(buf, start + strlen("OK+RECV:"), 2);
						buf[2] = 0;
						SysPrintf("TYPE=%d\n", ConvertStringToHex(buf));
				}
				else
				{
					UART0_Rx_cnt=0;
					return ErrCode_Success;
				}

				start = strstr(UART0_Rx_Buf, ",");
				if (start != NULL)
				{
						memcpy(buf, start + strlen(","), 2);
						buf[2] = 0;
						SysPrintf("PORT=%d\n", ConvertStringToHex(buf));
				}

				next = strstr(start + strlen(","), ",");
				if (start != NULL)
				{
					memcpy(buf, next + strlen(","), 2);
					buf[2] = 0;
					SysPrintf("LEN=%d\n", ConvertStringToHex(buf));

					datalength = ConvertStringToHex(buf);
				}

				if (datalength > 0)
				{
					memset(buf, 0, sizeof(buf));
					start = strstr(next + strlen(","), ",");
					if (start != NULL)
					{
						memcpy(buf, start + strlen(","), datalength * 2); 

						
					}
					i = 0;
					j = 0;
					for (i = 0; i < datalength * 2; i = i + 2)
					{
						Recebuffer[j++] = ConvertStringToHex(buf + i); 
					}
					

					if ((Recebuffer[2]==0x02)||(Recebuffer[2]==0x03)||(Recebuffer[2]==0x04))   
					{
						UART0_Rx_cnt=0;
						return ErrCode_ReceData;
					}
					else
					{
						UART0_Rx_cnt=0;
						return ErrCode_Success;	
					}
				}
				#endif
				UART0_Rx_cnt=0;
				return ErrCode_Success;

	}
	UART0_Rx_cnt=0;
	SysPrintf("receive fail\n");
	return ErrCode_Fail;
}



void MHL10_Sent_Packet_To_Gateway_Reboot(void) 
{
		
	Wakeup_MHL10_HW();
	HAL_Delay(50);
	SysPrintf("MHL10  Sent packet to Server to reboot...\r\n");
	UART0_SentData(MHL10_ADDR_Valid,8);
	HAL_Delay(4000);	
}




